<template>
  <div class="register-container">
    <el-form
      ref="registerForm"
      :model="registerForm"
      :rules="registerRules"
      class="register-form"
      autocomplete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">用户注册</h3>
        <router-link to="/login" style="float: right; padding: 3px 0">
          <el-button type="info" circle plain>返回登录</el-button>
        </router-link>
      </div>

      <el-form-item prop="studentId">
        <span class="svg-container">
          <i class="el-icon-user"></i>
        </span>
        <el-input
          ref="studentId"
          v-model="registerForm.studentId"
          placeholder="学号"
          name="studentId"
          type="text"
          tabindex="1"
          autocomplete="on"
        />
      </el-form-item>

      <el-form-item prop="nickname">
        <span class="svg-container">
          <i class="el-icon-user"></i>
        </span>
        <el-input
          ref="nickname"
          v-model="registerForm.nickname"
          placeholder="用户名"
          name="nickname"
          type="text"
          tabindex="2"
          autocomplete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <i class="el-icon-lock"></i>
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="registerForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="3"
          autocomplete="on"
        />
        <span class="show-pwd" @click="showPwd">
          <i :class="passwordType === 'password' ? 'el-icon-view' : 'el-icon-hide'"></i>
        </span>
      </el-form-item>

      <el-form-item prop="confirmPassword">
        <span class="svg-container">
          <i class="el-icon-lock"></i>
        </span>
        <el-input
          :key="confirmPasswordType"
          ref="confirmPassword"
          v-model="registerForm.confirmPassword"
          :type="confirmPasswordType"
          placeholder="确认密码"
          name="confirmPassword"
          tabindex="4"
          autocomplete="on"
        />
        <span class="show-pwd" @click="showConfirmPwd">
          <i :class="confirmPasswordType === 'password' ? 'el-icon-view' : 'el-icon-hide'"></i>
        </span>
      </el-form-item>

      <el-form-item prop="phone">
        <span class="svg-container">
          <i class="el-icon-phone"></i>
        </span>
        <el-input
          ref="phone"
          v-model="registerForm.phone"
          placeholder="手机号"
          name="phone"
          type="text"
          tabindex="5"
          autocomplete="on"
        />
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleRegister"
        plain
      >注册</el-button>
    </el-form>
  </div>
</template>

<script>
import authService from '@/services/auth.service'

export default {
  name: 'RegisterPage',
  data() {
    const validateStudentId = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入学号'))
      } else {
        callback()
      }
    }
    const validateNickname = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6位'))
      } else {
        if (this.registerForm.confirmPassword !== '') {
          this.$refs.registerForm.validateField('confirmPassword')
        }
        callback()
      }
    }
    const validateConfirmPassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    const validatePhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入手机号'))
      } else if (!/^1[3-9]\d{9}$/.test(value)) {
        callback(new Error('请输入正确的手机号'))
      } else {
        callback()
      }
    }
    return {
      registerForm: {
        studentId: '',
        nickname: '',
        password: '',
        confirmPassword: '',
        phone: ''
      },
      registerRules: {
        studentId: [{ required: true, trigger: 'blur', validator: validateStudentId }],
        nickname: [{ required: true, trigger: 'blur', validator: validateNickname }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        confirmPassword: [{ required: true, trigger: 'blur', validator: validateConfirmPassword }],
        phone: [{ required: true, trigger: 'blur', validator: validatePhone }]
      },
      passwordType: 'password',
      confirmPasswordType: 'password',
      loading: false
    }
  },
  methods: {
    showPwd() {
      this.passwordType = this.passwordType === 'password' ? '' : 'password'
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    showConfirmPwd() {
      this.confirmPasswordType = this.confirmPasswordType === 'password' ? '' : 'password'
      this.$nextTick(() => {
        this.$refs.confirmPassword.focus()
      })
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true
          
          // 调用注册接口
          authService.register({
            studentId: this.registerForm.studentId,
            nickname: this.registerForm.nickname,
            password: this.registerForm.password,
            phone: this.registerForm.phone
          }).then(response => {
            console.log('注册响应:', response)
            
            if (response.code === 200) {
              this.$message.success('注册成功，请登录')
              this.$router.push('/login')
            } else {
              this.$message.error(response.message || '注册失败')
            }
          }).catch(error => {
            console.error('注册错误:', error)
            this.$message.error(error.message || '注册失败，请检查网络连接')
          }).finally(() => {
            this.loading = false
          })
        } else {
          console.log('表单验证失败')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.register-container {
  min-height: 100%;
  width: 100%;
  background-color: #2d3a4b;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;

  .register-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;

    .title-container {
      position: relative;
      text-align: center;
      margin-bottom: 40px;

      .title {
        font-size: 26px;
        color: #eee;
        margin: 0;
        font-weight: bold;
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: #889aa4;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: #889aa4;
      cursor: pointer;
      user-select: none;
    }

    ::v-deep .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        background: transparent;
        border: 0;
        -webkit-appearance: none;
        border-radius: 0;
        padding: 12px 5px 12px 15px;
        color: #fff;
        height: 47px;
        caret-color: #fff;

        &:-webkit-autofill {
          box-shadow: 0 0 0 1000px #283443 inset !important;
          -webkit-text-fill-color: #fff !important;
        }
      }
    }

    ::v-deep .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
  }
}
</style> 